﻿using System;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using ET;
using BLL;

namespace ERP.PageUC
{
    public partial class MaterialsUC : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGridDrop();
            }
        }

        protected void btnSavePopup_Click(object sender, EventArgs e)
        {
            if (lbID.Value == "")
            {
                InsertMaterials();
            }
            else
            {
                UpdateMaterials();
            }
        }

        protected void btnReloadPopup_Click(object sender, EventArgs e)
        {
            BindGridDrop();
        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            var clsBrand = new Materials_BLL();
            var dtMaterials = clsBrand.GetMaterials();
            if (dtMaterials != null && dtMaterials.Rows.Count > 0)
            {
                foreach (DataRow dr in dtMaterials.Rows)
                {
                    if (txtMaterials_Alias.Text == dr["Materials_Alias"].ToString())
                    {
                        kiemtra = true;
                        if (Page.Master != null)
                            ((Label)Page.Master.FindControl("lbThongBao")).Text = "Trùng mã vật tư !";
                        OpenDialog("#thongbao");
                        if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                    else if (txtMaterials_Name.Text == dr["Materials_Name"].ToString())
                    {
                        kiemtra = true;
                        if (Page.Master != null)
                            ((Label)Page.Master.FindControl("lbThongBao")).Text = "Trùng tên vật tư !";
                        OpenDialog("#thongbao");
                        if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertMaterials()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new Materials_ET
                    {
                        Materials_Alias = txtMaterials_Alias.Text,
                        Materials_Name = txtMaterials_Name.Text,
                        Materials_Tankage = float.Parse(txtTankage.Text),
                        Unit_Id = int.Parse(drUnit.SelectedValue),
                        Materials_Type_Id = int.Parse(drMaterial_Type.SelectedValue)
                    };
                    Materials_BLL.InsertMaterials(obj);
                    ClearTextBox();
                    if (Page.Master != null)
                        ((Label)Page.Master.FindControl("lbThongBao")).Text = "Thêm mới thành công !";
                }
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                txtMaterials_Alias.Enabled = true;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }
        protected void UpdateMaterials()
        {
            try
            {
                var obj = new Materials_ET
                {
                    Materials_Id = int.Parse(lbID.Value),
                    Materials_Alias = txtMaterials_Alias.Text,
                    Materials_Name = txtMaterials_Name.Text,
                    Materials_Tankage = float.Parse(txtTankage.Text),
                    Unit_Id = int.Parse(drUnit.SelectedValue),
                    Materials_Type_Id = int.Parse(drMaterial_Type.SelectedValue)
                };
                Materials_BLL.UpdateMaterials(obj);
                ((Page.Materials)Page).GetMaterials();
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Sữa thành công !";
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                txtMaterials_Alias.Enabled = false;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }
        protected void BindGridDrop()
        {
            var clsNhomVatTu = new Materials_Type_BLL();
            var dtNhomVatTu = clsNhomVatTu.GetMaterials_Type();
            if (dtNhomVatTu != null && dtNhomVatTu.Rows.Count > 0)
            {
                drMaterial_Type.Items.Clear();
                drMaterial_Type.Items.Add(new ListItem("-Vui lòng chọn nhóm vật tư-", "-1"));
                drMaterial_Type.DataSource = dtNhomVatTu;
                drMaterial_Type.DataValueField = "Materials_Type_Id";
                drMaterial_Type.DataTextField = "Materials_Type_Name";
                drMaterial_Type.DataBind();
            }

            var clsDVT = new Unit_BLL();
            var dtDVT = clsDVT.GetUnit();
            if (dtDVT != null && dtDVT.Rows.Count > 0)
            {
                drUnit.Items.Clear();
                drUnit.Items.Add(new ListItem("-Vui lòng chọn đơn vị tính-", "-1"));
                drUnit.DataSource = dtDVT;
                drUnit.DataValueField = "Unit_Id";
                drUnit.DataTextField = "Unit_Name";
                drUnit.DataBind();
            }
        }

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            var sb = new StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                "scriptKey", sb.ToString(), true);
        }
        #endregion
        #region[ClearTextbox]
        protected void ClearTextBox()
        {
            txtMaterials_Alias.Text = string.Empty;
            txtMaterials_Name.Text = string.Empty;
            txtTankage.Text = string.Empty;
            drMaterial_Type.SelectedIndex = 0;
            drUnit.SelectedIndex = 0;
        }
        #endregion
    }
}